package com.xiaolong.sojiaziademo;

import android.app.Application;
import android.content.Context;
import android.os.Build;

import com.xiaolong.sojiaziademo.dump.LowSdkDump;
import com.xiaolong.sojiaziademo.dump.OreoDump;
import com.xiaolong.sojiaziademo.util.DeviceUtils;
import com.xiaolong.sojiaziademo.util.Logger;

import java.io.File;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import xcrash.XCrash;

public class HookLogic extends Application implements IXposedHookLoadPackage {
    private static final String TAG = HookLogic.class.getName();

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // 加入 进程崩溃 监听 https://gitee.com/caikelun/xCrash.git
       XCrash.init(this);
    }

    @Override
    public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpparam) {
        PackerInfo.Type type = PackerInfo.find(lpparam);
        if (type == null) {
            return;
        }
        final String packageName = lpparam.packageName;
        if (lpparam.packageName.equals(packageName)) {
            String path = "/data/data/" + packageName + "/dump";
            Logger.log(TAG, path);
            File parent = new File(path);
            if (!parent.exists() || !parent.isDirectory()) {
                parent.mkdirs();
            }
            Logger.log(TAG, "sdk version:" + Build.VERSION.SDK_INT);
            if (DeviceUtils.isOreo() || DeviceUtils.isPie() || DeviceUtils.isQ()) {
                Logger.log(TAG, " OreoDump :: init-1 > " + lpparam);
                OreoDump.init(lpparam);
                Logger.log(TAG, " OreoDump :: init-2 > " + lpparam);
            } else {
                Logger.log(TAG, " LowSdkDump :: init-1 > " + lpparam);
                LowSdkDump.init(lpparam, type);
                Logger.log(TAG, " LowSdkDump :: init-2 > " + lpparam);
            }

        }
    }
}
